package com.orhon.smartcampus.modules.baseinfo.mutation;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.baseinfo.dao.TimetableItemDao;
import com.orhon.smartcampus.modules.baseinfo.entity.TimetableItem;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class DeleteTimetableItemMutation implements DataFetcher {


    @Autowired
    TimetableItemDao timetableItemDao;

    /**
     * 删除作息项
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {
        if (environment.getArgument("id") != null) {
            List<Integer> ids = environment.getArgument("id");
            ids.forEach(id -> {
                        TimetableItem item = timetableItemDao.get(TimetableItem.class, Long.valueOf(id));
                        item.setDeletedAt(new Date());
                        try {
                            timetableItemDao.update(item);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
            );
            return Result.ok();
        }
        return Result.error();
    }
}

